
***********************************************
*RD illustration using rdrobust_senate.dta
***********************************************

use "C:\Users\zhangchuanchuan\Desktop\RD\rdrobust_senate.dta", clear

sum vote margin class termshouse termssenate population

***** Graphical analysis********

*1. Jump at the cutoff*

* binscatter
binscatter vote margin,rd(0) n(50) linetype(lfit) ///
                       xtitle("Vote Share in Election at time t") ///
					   ytitle("Vote Share in Election at time t+2")

binscatter vote margin if margin>-50 & margin<50, ///
                       rd(0) n(50) linetype(lfit) ///
					   xtitle("Vote Share in Election at time t") ///
					   ytitle("Vote Share in Election at time t+2")
* cmogram

cmogram vote margin if margin>-50 & margin<50, ///
     scatter cut(0) lineat(0) lfit ci(95) histopts(bin(25))

* twoway graph

tw (scatter vote margin if margin>-50 & margin<50) ///
   (lfit vote margin if margin>-50 & margin<0) ///
   (lfit vote margin if margin>=0 & margin<50, ///
                       xline(0,lc(red)) legend(off) ///
                       xtitle("Vote Share in Election at time t") ///
                       ytitle("Vote Share in Election at time t+2"))

* rdplot
					   
rdplot vote margin, c(0) p(4) binselect(es) ci(95) ///
       graph_options(title("RD Plot: U.S. Senate Election Data") ///
                     ytitle(Vote Share in Election at time t+2) ///
                     xtitle(Vote Share in Election at time t) ///
                     graphregion(color(white)))

rdplot vote margin if margin>-50 & margin<50, c(0) p(2) binselect(es) ci(95) ///
       graph_options(title("RD Plot: U.S. Senate Election Data") ///
                     ytitle(Vote Share in Election at time t+2) ///
                     xtitle(Vote Share in Election at time t) ///
                     graphregion(color(white)))
					 
*2. Manipulation of the running variable

DCdensity margin,breakpoint(0) generate(Xj Yj r0 fhat se_fhat)

*Generated variables by DCdensity
*"1. Name of variable in which to store cell midpoints of histogram"
*"2. Name of variable in which to store cell heights of histogram"
*"3. Name of variable in which to store evaluation sequence for local linear regression loop"
*"4. Name of variable in which to store local linear density estimate"
*"5. Name of variable in which to store standard error of local linear density estimate"

*3. Other factors smoothly change at the cutoff
cmogram population margin if margin>-50 & margin<50, ///
                  scatter cut(0) lineat(0) qfit ci(95) histopts(bin(20)) 

***** Estimation********

g D = margin>0 // generate treatment status
g marginsq = margin*margin
g D_margin = D*margin

reg vote D margin
reg vote D margin marginsq

reg vote D margin D_margin

rdrobust vote margin

reg vote D margin if margin>-17 & margin<17
reg vote D margin D_margin if margin>-17 & margin<17






